package com.cyandpos.pad.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.changyi.device.printer.PrintManagerLib;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.Constant;
import com.cyandpos.pad.common.commondata.ErpCommonData;

import com.cyandpos.pad.common.ticketformat.TicketFormat;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.O2OPayDetail;
import com.cyandpos.pad.posdata.Order;
import com.cyandpos.pad.posdata.PayInfo;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.RefundOrder;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.posdata.YhkCardItem;
import com.cyandpos.pad.recycleritemanim.ExpandableViewHoldersUtil;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.tradedetails.adapter.OrderqueeryGoodsListAdapter;
import com.cyandpos.pad.tradedetails.adapter.TimeThread;
import com.cyandpos.pad.tradedetails.presenter.TradeDetailsPresenter;
import com.cyandpos.pad.tradedetails.presenter.TradeDetailsPresenterImpl;
import com.cyandpos.pad.tradedetails.view.TradeDetailsView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.InputNumberDialog;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.ToastUtils;
import com.cyandpos.pad.utils.view.MyListView;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.cyandpos.pad.utils.OrderType.DJDORDER;
import static com.cyandpos.pad.utils.OrderType.PTORDER;
import static com.cyandpos.pad.utils.OrderType.YFKORDER;

public class OrderqueryActivity extends  BaseActivity implements View.OnClickListener, TradeDetailsView {

    @BindView(R.id.iv_main_project_logo)
    ImageView ivMainProjectLogo;
    @BindView(R.id.btn_orderquery_exit_system)
    Button btn_orderquery_exit;
    @BindView(R.id.btn_queryorder)
    Button btn_queryorder;
    @BindView(R.id.btn_queryorder_th)
    Button btn_queryorderth;
    @BindView(R.id.rv_orderquery_goodList)
    RecyclerView rvorderquerygoodList;

    @BindView(R.id.order_query_edit_start)
    TextView orderqueryedit_start;
    @BindView(R.id.order_query_edit_end)
    TextView orderqueryedit_end;
    @BindView(R.id.order_query_edit)
    TextView orderquery_edit;
    @BindView(R.id.tv_goods_totalNum)
    TextView tvgoods_totalNum;
    @BindView(R.id.tv_goods_totalmoney)
    TextView tvgoods_totalmoney;
    @BindView(R.id.tv_goods_totalmoney2)
    TextView tvgoods_totalmoney2;
     @BindView(R.id.tv_main_title_orderno)
    TextView tv_title_orderno;
     @BindView(R.id.tv_codequery_title_sktno)
    TextView tvcodequery_title_sktno;
    @BindView(R.id.tv_querycode_title_cashier)
    TextView tvquerycode_title_cashier;
   @BindView(R.id.tv_details_title_version)
    TextView tvdetails_title_version;
   @BindView(R.id.tv_order_title_currentTime)
    public  TextView tv_title_currentTime;
    @BindView(R.id.switch_status_order)
    Spinner switchstatus; //去支付
    @BindView(R.id.btn_queryorder_cd)
    Button btn_queryordercd;
    @BindView(R.id.page_up)
    Button pageup;
    @BindView(R.id.page_down)
    Button pagedown;
    public  int  selectPosition=-1;
    private final OrderqueryActivity.MyHandler handler = new OrderqueryActivity.MyHandler(this);
    private TradeDetailsPresenter mTradeDetailsPresenter;
    private QueryOrderReq orderReq;
    private List<Order> tempList=new ArrayList<Order>();
    private OrderqueeryGoodsListAdapter orderReqAdapter;
    private ProgressDialog dialog;
    private String jlbh,orderNo;
    public static AlertDialog mDialog;//所有dialog
    private ExpandableViewHoldersUtil.KeepOneHolder<OrderqueryActivity.ViewHolder> keepOne;
    private List<String> list_status;
    private int pageSize = 5;
    private int pageinx = 1;  // 用于显示页号的索引

    // 用于显示每列5个Item项。
    int VIEW_COUNT = 5;
    private MyAdapter myAdapter;

    @Override
    protected void initData() {
        btn_orderquery_exit.setOnClickListener(this);
        btn_queryorder.setOnClickListener(this);
        Picasso.get().load(R.mipmap.shiji_log1).
                networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
                .placeholder(R.mipmap.shiji_log1).error(R.mipmap.shiji_log1)
                .into(ivMainProjectLogo);
        mTradeDetailsPresenter = new TradeDetailsPresenterImpl(this);
        dialog = new ProgressDialog(OrderqueryActivity.this);
        dialog.setCancelable(false);
        dialog.setMessage("正在查询明细，请稍等 ....");
        orderqueryedit_start.setOnClickListener(this);
        orderqueryedit_end.setOnClickListener(this);
        orderquery_edit.setOnClickListener(this);
        btn_queryorderth.setOnClickListener(this);
        pageup.setOnClickListener(this);
        pagedown.setOnClickListener(this);
        btn_queryordercd.setOnClickListener(this);
        orderqueryedit_start.setText(new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
        orderqueryedit_end.setText(new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()));
        tv_title_orderno .setText(ErpCommonData.orderNo + "");
        tvcodequery_title_sktno .setText(ErpCommonData.sktno + "");
        tvquerycode_title_cashier.setText(ErpCommonData.loadConfig.getCashierName() + "");
        tvdetails_title_version.setText(CommonUtils.getCurrentVersionName(OrderqueryActivity.this));
        orderReq = new QueryOrderReq(); //添加默认 查询
        orderReq.setBeginTime(orderqueryedit_start.getText().toString()+" "+"00:00:00");
        orderReq.setEndTime(orderqueryedit_end.getText().toString()+" "+"23:59:59");
         orderReq.setSktno(ErpCommonData.sktno);
        orderReq.setStatus("1");
        orderReq.setPageNo(1);
        orderReq.setPageSize(pageSize);
           orderReq.setQueryHistoryData(true);
        //   orderReq.setNotQueryDetail(false);
        mTradeDetailsPresenter.QueryTradeDetails(orderReq);//默认今天
        TimeThread timeThread = new TimeThread(handler);
        timeThread.setDaemon(true);//守护线程
        timeThread.start();

        ExpandableViewHoldersUtil.getInstance().init().setNeedExplanedOnlyOne(true);
        rvorderquerygoodList.setLayoutManager(new LinearLayoutManager(this));
        //清空记录展开还是关闭的缓存数据
        ExpandableViewHoldersUtil.getInstance().resetExpanedList();
        list_status = new ArrayList<>();
        list_status.add("普通");
        list_status.add("订金单");
        ArrayAdapter<String> adapter_kc_address = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list_status);
        adapter_kc_address.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        switchstatus.setAdapter(adapter_kc_address);
        myAdapter = new MyAdapter(tempList);
        rvorderquerygoodList.setAdapter(myAdapter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_orderquery;
    }


    public static class MyHandler extends Handler {
        private final WeakReference<OrderqueryActivity> mActivity;

        public MyHandler(OrderqueryActivity activity) {
            this.mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            OrderqueryActivity orderqueryActivity = mActivity.get();
            if (orderqueryActivity == null) {
                return;
            }
            switch (msg.what) {
                case Constant.MAIN_UPDATE_CURRENT_TIME_HANDLEID: {
                    String time = msg.getData().getString("time");
                    orderqueryActivity.tv_title_currentTime.setText(time);
                    break;
                }
                default:
                    break;
            }
        }

    }
    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.btn_orderquery_exit_system:
                    finish();
             break;
             case R.id.order_query_edit_start:

                 final Calendar c = Calendar.getInstance();     //创建日期选择对象
                 DatePickerDialog dialog_start = new DatePickerDialog(OrderqueryActivity.this, new DatePickerDialog.OnDateSetListener() {
                     @Override
                     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                         c.set(year, monthOfYear, dayOfMonth);
                         orderqueryedit_start.setText(DateFormat.format("yyy-MM-dd", c));
                     }
                 }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
                 dialog_start.show();
             break;
             case R.id.order_query_edit_end:
                  final Calendar c1 = Calendar.getInstance();     //创建日期选择对象
                 DatePickerDialog dialog_end = new DatePickerDialog(OrderqueryActivity.this, new DatePickerDialog.OnDateSetListener() {
                     @Override
                     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                         c1.set(year, monthOfYear, dayOfMonth);
                         orderqueryedit_end.setText(DateFormat.format("yyy-MM-dd", c1));
                     }
                 }, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH));
                 dialog_end.show();

             break;
             case R.id.order_query_edit:
                 InputNumberDialog numberDialog = InputNumberDialog.getInputNumberDialog(OrderqueryActivity.this, new InputNumberDialog.InputDialogOnSubmitClickListener() {
                     @Override
                     public void InputDialogOnSubmitClick(String content) {
                         if (CommonUtils.isNullOrEmpty(content)){
                             ToastUtils.showMsg("输入数据不能为空");
                             return;
                         }
                         orderquery_edit.setText(content);
                         orderReq = new QueryOrderReq();
                         orderReq.setSktno(ErpCommonData.sktno);
                         orderReq.setQueryHistoryData(true);
                         orderReq.setOrderNo(orderquery_edit.getText().toString());
                         mTradeDetailsPresenter.QueryTradeDetails(orderReq);
                     }
                 });
                 numberDialog.setIsCanInputPoint(false);
                 numberDialog.setTitle("订单号");
                 numberDialog.setEtHint("请输入订单号");
                 numberDialog.showDialog(false);
                 break;
             case R.id.btn_queryorder:
                 orderReq = new QueryOrderReq();  //把查询的条件
                 orderReq.setBeginTime(orderqueryedit_start.getText().toString()+" "+"00:00:00");
                 orderReq.setEndTime(orderqueryedit_end.getText().toString()+" "+"23:59:59");
                 orderReq.setStatus(YFKORDER);
                 if (list_status.get(switchstatus.getSelectedItemPosition()).equals("订金单")){
                     orderReq.setOrderFunction(DJDORDER);
                 }else if (list_status.get(switchstatus.getSelectedItemPosition()).equals("普通")){
                     orderReq.setOrderFunction(PTORDER);
                 }
                 orderReq.setPageNo(1);
                 orderReq.setPageSize(5);
                 orderReq.setQueryHistoryData(true);
                 orderReq.setSktno(ErpCommonData.sktno);
                 mTradeDetailsPresenter.QueryTradeDetails(orderReq);//默认今天


                 break;
             case R.id.btn_queryorder_cd: { //重打小票
                 if (selectPosition<0){
                     Toast.makeText(OrderqueryActivity.this,"请选择重打单号!",Toast.LENGTH_SHORT).show();
                     return;
                 }
                 List<String> list = TicketFormat.createTicketFormat(getTicketInfo(), true,-1);
                 PrintManagerLib.initPrinter(OrderqueryActivity.this, CommonUtils.getSystemModel())
                         .initDevice()
                         .printList(list);
                 selectPosition=-1; //重打完 这个值重置
             }
             break;
             case R.id.btn_queryorder_th:

                 if (selectPosition<0){
                     Toast.makeText(this,"请选择单号",Toast.LENGTH_SHORT).show();
                     return;
                 }
                 Intent intent=new Intent(OrderqueryActivity.this,RefundActivity.class);
                 intent.putExtra("TicketInfo",getTicketInfo());
                 intent.putExtra("old_sktno",tempList.get(selectPosition).getSktno());
                 intent.putExtra("old_orderNo",tempList.get(selectPosition).getOrderNo());
                 // intent.putExtra("managerNo",managerNo);
                 intent.putExtra("isAllOrder",false);//是否整单退货
                 startActivity(intent);
                 selectPosition=-1; //退货完这个值重置
                 break;
             case R.id.page_up: //上一页
                 pageinx--;
                 checkButton();
                 break;
             case R.id.page_down://下一页
                 pageinx++;
                 checkButton();
                 break;
         }
    }

    public void checkButton() {
        // 索引值小于等于0，表示不能向前翻页了，以经到了第一页了。
        // 将向前翻页的按钮设为不可用。
        if (pageinx <= 0) {
            pageup.setEnabled(false); //上一页不能点的时候
            pageinx+=1;
            pagedown.setEnabled(true); //下一页是可以点的

        }else{
            pageup.setEnabled(true);

            orderReq = new QueryOrderReq(); //添加默认 查询
            orderReq.setBeginTime(orderqueryedit_start.getText().toString()+" "+"00:00:00");
            orderReq.setEndTime(orderqueryedit_end.getText().toString()+" "+"23:59:59");
            orderReq.setSktno(ErpCommonData.sktno);
            orderReq.setStatus("1");
            orderReq.setPageNo(pageinx);
            orderReq.setPageSize(pageSize);
            orderReq.setQueryHistoryData(true);
            mTradeDetailsPresenter.QueryTradeDetails(orderReq);//默认今天
        }
        // 值的长度减去前几页的长度，剩下的就是这一页的长度，如果这一页的长度比View_Count小，表示这是最后的一页了，后面在没有了。
        // 将向后翻页的按钮设为不可用。
        if (tempList.size() <VIEW_COUNT) {
            pagedown.setEnabled(false);
        }
        // 否则将2个按钮都设为可用的。
        else {
            pagedown.setEnabled(true);
        }

    }


    //查询返回的数据
    @Override
    public void showProgress() {
        dialog.show();
    }
    @Override
    public void hideProgress() {
        dialog.dismiss();
    }

    private TicketInfo getTicketInfo() {
        TicketInfo ticketInfo= new TicketInfo();
        ticketInfo.setTotalAmount(tempList.get(selectPosition).getTotalAmount());
        ticketInfo.setTotalDiscount(tempList.get(selectPosition).getTotalDiscount());
        ticketInfo.setChangeAmount(tempList.get(selectPosition).getChangeAmount());
        ticketInfo.setSubTickets(tempList.get(selectPosition).getSubTickets());
        ticketInfo.setOfferCouponList(tempList.get(selectPosition).getOfferCouponList());
        ticketInfo.setPayList(tempList.get(selectPosition).getPayList());
        ticketInfo.setLottoList(tempList.get(selectPosition).getLottoList());
        ticketInfo.setCasherInfo(tempList.get(selectPosition).getCasherInfo());
        ticketInfo.setMemberInfo(tempList.get(selectPosition).getMemberInfo());
        ticketInfo.setJlbh(tempList.get(selectPosition).getJlbh());
        ticketInfo.setOrderNo(tempList.get(selectPosition).getOrderNo());
        ticketInfo.setOrderTime(tempList.get(selectPosition).getOrderTime());
        if (tempList.get(selectPosition).getPayList().size()>0){
            for (Payment payment:tempList.get(selectPosition).getPayList()){
                if (payment.getPayInfoList().size()>0){
                    for (PayInfo payInfo:payment.getPayInfoList()){
                        if (payment.getPayType()==Payment.SKFS_WX){
                            O2OPayDetail o2o=new O2OPayDetail();
                            o2o.setTotalFee(MoneyUtils.getMoneyInt(payInfo.getPay_amount()));
                            o2o.setOutTradeNo(payInfo.getOutTradeNo());
                            o2o.setTransactionId(payInfo.getTransactionId());
                            o2o.setSktno(tempList.get(selectPosition).getSktno());
                            o2o.setJysj(payInfo.getPay_time());
                            o2o.setPayCode(payment.getPayCode());
                            ticketInfo.getO2oWxList().add(o2o);
                        }else if (payment.getPayType()==Payment.SKFS_ZFB){
                            O2OPayDetail o2o=new O2OPayDetail();
                            o2o.setTotalFee(MoneyUtils.getMoneyInt(payInfo.getPay_amount()));
                            o2o.setOutTradeNo(payInfo.getOutTradeNo());
                            o2o.setTransactionId(payInfo.getTransactionId());
                            o2o.setSktno(tempList.get(selectPosition).getSktno());
                            o2o.setJysj(payInfo.getPay_time());
                            o2o.setPayCode(payment.getPayCode());
                            ticketInfo.getO2oZfbList().add(o2o);
                        }else if(payment.getPayType()==Payment.SKFS_YHK ){
                            YhkCardItem item= new YhkCardItem();
                            item.setAmount(payInfo.getPay_amount());
                            item.setLsh(payInfo.getPay_lsh());
                            item.setCkh(payInfo.getPay_ckh());
                            item.setPzh(payInfo.getPay_pzh());
                            item.setCardNo(payInfo.getPay_cardNo());
                            item.setType(payment.getPayType()+"");
                            ticketInfo.getBankCardList().add(item);
                        }else if(payment.getPayType()==Payment.SKFS_JH ){
                            if (payment.getPayCode() ==2){
                                O2OPayDetail o2o=new O2OPayDetail();
                                o2o.setTotalFee(MoneyUtils.getMoneyInt(payInfo.getPay_amount()));
                                o2o.setOutTradeNo(payInfo.getOutTradeNo());
                                o2o.setTransactionId(payInfo.getTransactionId());
                                o2o.setSktno(tempList.get(selectPosition).getSktno());
                                o2o.setJysj(payInfo.getPay_time());
                                o2o.setPayCode(payment.getPayCode());
                                ticketInfo.getO2oZfbList().add(o2o);
                            }else if (payment.getPayCode() ==3){
                                O2OPayDetail o2o=new O2OPayDetail();
                                o2o.setTotalFee(MoneyUtils.getMoneyInt(payInfo.getPay_amount()));
                                o2o.setOutTradeNo(payInfo.getOutTradeNo());
                                o2o.setTransactionId(payInfo.getTransactionId());
                                o2o.setSktno(tempList.get(selectPosition).getSktno());
                                o2o.setJysj(payInfo.getPay_time());
                                o2o.setPayCode(payment.getPayCode());
                                ticketInfo.getO2oWxList().add(o2o);
                            }
                        }
                    }
                }
            }
        }
        return ticketInfo;
    }
        @Override
    public void onTradeDetailsSuccess(List<Order> tradeDetailslist, double orderCount, RefundOrder data) {
            if (tradeDetailslist == null || tradeDetailslist.size() == 0) {
            ToastUtils.showMsg( "无销售信息");
            return;
        }
        tempList.clear();
        tempList.addAll(tradeDetailslist);
        myAdapter.notifyDataSetChanged();
            int  totlemoney=0;
        for (int i=0;i<tradeDetailslist.size();i++){
            totlemoney+=tradeDetailslist.get(i).getTotalMoneyOut();
        }
        tvgoods_totalNum.setText("共:"+orderCount+" 单");
        tvgoods_totalmoney.setText("总支付金额:"+MoneyUtils.getMoneyStr(totlemoney));
        tvgoods_totalmoney2.setText("总金额："+MoneyUtils.getMoneyStr(totlemoney));
    }
    @Override
    public void showLoadFailMsg(String errorMsg) {
        orderquery_edit.setText("");
        Toast.makeText(this, "查询交易明细失败" + errorMsg, Toast.LENGTH_LONG).show();

    }
    public void showQueryMemberDialog(final Activity mContext) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_queryorder, null);
        TextView tvName = view.findViewById(R.id.tv_dialog_title_name);
        final SelfNumKeyView sf_containt = view.findViewById(R.id.queryorder_keyboard);
         EditText   order_query= view.findViewById(R.id.order_query_dialog);

        sf_containt.setEnterReceptor(order_query);
        sf_containt.setIsCanInputPoint(true);
        sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
                if (CommonUtils.isNullOrEmpty(num)){
                    ToastUtils.showMsg("订单号不能为空");
                    return;
                }

                orderquery_edit.setText(num);
                orderReq = new QueryOrderReq();
                orderReq.setSktno(ErpCommonData.sktno);
                orderReq.setOrderNo(orderquery_edit.getText().toString());
                mTradeDetailsPresenter.QueryTradeDetails(orderReq);//
            }
        });

        CommonUtils.setNoSoftware(order_query, OrderqueryActivity.this, true);

        tvName.setText("输入订单号查询");
        //关闭按钮
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();

            }
        });

        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }


    class MyAdapter extends RecyclerView.Adapter<OrderqueryActivity.ViewHolder> {
        private  List<Order> tempList1=new ArrayList<>();

        public MyAdapter(List<Order> tempList) {
            super();
            this.tempList1=tempList;
        }
        @Override
        public int getItemCount() {
            return tempList1.size();

           /* // ori表示到目前为止的前几页的总共的个数。
            int ori = VIEW_COUNT * pageinx;

            // 值的总个数-前几页的个数就是这一页要显示的个数，如果比默认的值小，说明这是最后一页，只需显示这么多就可以了
            if (tempList1.size() - ori < VIEW_COUNT) {
                return tempList1.size() - ori;
            }
            // 如果比默认的值还要大，说明一页显示不完，还要用换一页显示，这一页用默认的值显示满就可以了。
            else {
                return VIEW_COUNT;
            }*/



        }

        @NonNull
        @Override
        public OrderqueryActivity.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(OrderqueryActivity.this).inflate(R.layout.item_user_concern_layout, viewGroup, false);
            return new OrderqueryActivity.ViewHolder(view);
        }
        @Override
        public void onBindViewHolder(@NonNull final OrderqueryActivity.ViewHolder viewHolder, final int position) {


            keepOne.bind(viewHolder, position);

            viewHolder.liner_titlea.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                 /*   if(ExpandableViewHoldersUtil.isExpaned(position)){
                        viewHolder.contentTv.setMaxLines(3);
                    }else {
                        viewHolder.contentTv.setMaxLines(100);
                    }*/

                       keepOne.toggle(viewHolder);
                       selectPosition=position;
                    //商品列表
                    ArrayList<HashMap<String, Object>> goodslist = new ArrayList<HashMap<String, Object>>();

                    for (Goods goods: tempList1.get(position).getSubTickets().get(0).getGoodsList()) {
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("name", goods.getName());
                        map.put("price", MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(goods.getPrice())));
                        map.put("count", goods.getSaleCount());
                        map.put("preferential", MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(goods.getDiscount())));
                        map.put("amtmoneey", MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(goods.getSaleMoney())-MoneyUtils.getMoneyInt(goods.getDiscount())));
                        goodslist.add(map);
                    }

                    SimpleAdapter goodsAdapter = new SimpleAdapter(OrderqueryActivity.this, goodslist,
                            R.layout.lvitem_goods_tradedetails,
                            new String[] { "name", "price", "count","preferential","amtmoneey"}, new int[] {
                            R.id.tv_goodsname_tradedetails, R.id.tv_goodsprice_tradedetails, R.id.tv_goodscount_tradedetails, R.id.tv_goodspreferential_tradedetails,R.id.tv_goodsmoney_tradedetails});
                    viewHolder.rvorderdetails_goodList.setAdapter(goodsAdapter);

                    //收款方式列表
                    ArrayList<HashMap<String, Object>> payMents = new ArrayList<HashMap<String, Object>>();
                    int i=1;
                    for (Payment payment:tempList1.get(position).getPayList()) {

                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("num", i);
                        map.put("name", payment.getPayName());
                        map.put("skje", MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(payment.getPayAmount())));
                        map.put("paylsh", "0");
                        payMents.add(map);
                        i++;
                    }
                    SimpleAdapter paymentAdapter = new SimpleAdapter(OrderqueryActivity.this, payMents,
                            R.layout.lvitem_payment_tradedetails,
                            new String[] { "num", "name", "skje","paylsh" }, new int[] {
                            R.id.tv_goodetailsnum, R.id.tv_paymentname, R.id.tv_salemoney_tradedetails, R.id.tv_paylsh_tradedetails });
                    viewHolder.lv_skfs.setAdapter(paymentAdapter);

                    DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(OrderqueryActivity.this, DividerItemDecoration.VERTICAL);
                    mDividerItemDecoration.setDrawable(new ColorDrawable(ContextCompat.getColor(OrderqueryActivity.this,R.color.main_good_info_bar)));
                }
            });
/*
            viewHolder.lvArrorwBtn.setOnClickListener(new View.OnClickListener() {  //下拉箭头
                @Override
                public void onClick(View v) {
                    keepOne.toggle(viewHolder);
                }
            });*/

            viewHolder.tvInx.setText((viewHolder.getAdapterPosition()+1)+"");
            viewHolder.tvCoderq.setText(tempList1.get(position).getOrderTime());
            viewHolder.tvorder.setText(tempList1.get(position).getOrderNo());
            viewHolder.tvPrice.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt(tempList1.get(position).getTotalAmount())));
            if (tempList1.get(position).getOrderFunction()==1){
                viewHolder.tvSaleMname.setText("订金单");
            }else {
                viewHolder.tvSaleMname.setText("普通订单");
            }            viewHolder. tvDiscount.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt("   "+tempList1.get(position).getTotalDiscount())));//折扣
            viewHolder.tvSaleMoney.setText(MoneyUtils.getMoneyStr(MoneyUtils.getMoneyInt("   "+tempList1.get(position).getTotalAmount())));
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder implements ExpandableViewHoldersUtil.Expandable {
        TextView tvTitle;
        TextView tvInx;
        ImageView arrowImage;
        LinearLayout lvArrorwBtn;
        LinearLayout lvLinearlayout;
        LinearLayout liner_titlea;
        TextView contentTv;
        TextView tvCoderq;
        TextView tvorder;
        TextView tvPrice;
        TextView tvDiscount;
        TextView tvSaleMoney ;
        TextView tvSaleMname ;
        MyListView rvorderdetails_goodList;
        ListView lv_skfs;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvTitle = itemView.findViewById(R.id.item_user_concern_title);
            lvLinearlayout = itemView.findViewById(R.id.item_user_concern_link_layout);
            liner_titlea = itemView.findViewById(R.id.liner_title);
            lvArrorwBtn = itemView.findViewById(R.id.item_user_concern_arrow);
            arrowImage = itemView.findViewById(R.id.item_user_concern_arrow_image);
         //   contentTv = itemView.findViewById(R.id.item_user_concern_link_text);

             tvInx = itemView.findViewById(R.id.tv_sale_good_ticket_inx);
             tvCoderq = itemView.findViewById(R.id.tv_sale_good_rq);
             tvorder = itemView.findViewById(R.id.tv_sale_good_order);
             tvPrice = itemView.findViewById(R.id.tv_sale_good_price);
             tvDiscount = itemView.findViewById(R.id.tv_sale_good_discountMoney);
             tvSaleMoney = itemView.findViewById(R.id.tv_sale_good_saleMoney);
             tvSaleMname = itemView.findViewById(R.id.tv_sale_person_name);

            rvorderdetails_goodList=itemView.findViewById(R.id.rv_orderdetails_goodList);
            lv_skfs=itemView.findViewById(R.id.rv_orderquery_g);
             keepOne = ExpandableViewHoldersUtil.getInstance().getKeepOneHolder();

            lvLinearlayout.setVisibility(View.GONE);
            lvLinearlayout.setAlpha(0);
        }

        @Override
        public View getExpandView() {
            return lvLinearlayout;
        }

        @Override
        public void doCustomAnim(boolean isOpen) {
            if (isOpen) {
                ExpandableViewHoldersUtil.getInstance().rotateExpandIcon(arrowImage, 180, 0);
            } else {
                ExpandableViewHoldersUtil.getInstance().rotateExpandIcon(arrowImage, 0, 180);
            }
        }
    }


}
